package com.gps.skyrc.tool;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.gps.skyrc.R;


public class MyTitleBar extends RelativeLayout {

    private OnLeftButtonClickListener mLeftButtonClickListener;
    private OnRightButtonClickListener mRightButtonClickListener;

    private MyViewHolder mViewHolder;

    private View viewAppTitle;

    public MyTitleBar(Context context) {
        this(context, null);
    }

    public MyTitleBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MyTitleBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        viewAppTitle = inflater.inflate(R.layout.title_bar, null);
        this.addView(viewAppTitle, layoutParams);
        mViewHolder = new MyViewHolder(this);
        mViewHolder.leftRl.setOnClickListener(v -> {
            if (mLeftButtonClickListener != null) {
                mLeftButtonClickListener.onLeftButtonClick(v);
            }
        });

        mViewHolder.rightRl.setOnClickListener(v -> {
            if (mRightButtonClickListener != null) {
                mRightButtonClickListener.OnRightButtonClick(v);
            }
        });
    }

    public void initViewsVisible(boolean leftLy, boolean centerTv, boolean rightLy, boolean
            leftIv, boolean rightTv, boolean line) {
        mViewHolder.line.setVisibility(line ? View.VISIBLE : View.GONE);
        mViewHolder.leftRl.setVisibility(leftLy ? View.VISIBLE : View.INVISIBLE);
        mViewHolder.leftIv.setVisibility(leftIv ? View.VISIBLE : View.GONE);
        mViewHolder.leftTv.setVisibility(!leftIv ? View.VISIBLE : View.GONE);
        mViewHolder.centerTv.setVisibility(centerTv ? View.VISIBLE : View.GONE);
        mViewHolder.rightRl.setVisibility(rightLy ? View.VISIBLE : View.INVISIBLE);
        mViewHolder.rightTv.setVisibility(rightTv ? View.VISIBLE : View.GONE);
        mViewHolder.rightIv.setVisibility(!rightTv ? View.VISIBLE : View.GONE);

    }
    private static class MyViewHolder {
        RelativeLayout leftRl;
        ImageView leftIv;
        TextView centerTv;
        RelativeLayout rightRl;
        ImageView rightIv;
        TextView rightTv, leftTv;
        View line;
        RelativeLayout mTitleBarLl;

        private MyViewHolder(View v) {
            leftRl = v.findViewById(R.id.llLeftGoBack);
            centerTv = v.findViewById(R.id.tvCenterTitle);
            rightRl = v.findViewById(R.id.llRight);
            rightIv = v.findViewById(R.id.ivRightComplete);
            rightTv = v.findViewById(R.id.tvRightComplete);
            leftIv = v.findViewById(R.id.ivlLeftGoBack);
            leftTv = v.findViewById(R.id.tvLeftCancel);
            line = v.findViewById(R.id.divider);
            mTitleBarLl = v.findViewById(R.id.title_bar_rl);
        }
    }

    public void setRightTvVisibility(boolean visibility){
        mViewHolder.rightRl.setVisibility(visibility ? VISIBLE : INVISIBLE);
        mViewHolder.rightTv.setVisibility(visibility ? VISIBLE : INVISIBLE);
        mViewHolder.rightIv.setVisibility(GONE);
    }
    public void setRightIvVisibility(boolean visibility){
        mViewHolder.rightRl.setVisibility(visibility ? VISIBLE : INVISIBLE);
        mViewHolder.rightTv.setVisibility(GONE);
        mViewHolder.rightIv.setVisibility(visibility ? VISIBLE : INVISIBLE);
    }

    public void setRightClickable(boolean isEnable){
        mViewHolder.rightRl.setEnabled(isEnable);
    }
    /**
     * 设置标题
     *
     * @param title
     */
    public void setAppTitle(CharSequence title) {
        if (!TextUtils.isEmpty(title)) {
            mViewHolder.centerTv.setText(title);
        }
    }

    /**
     * 设置标题
     *
     * @param title
     */
    public void setAppTitle(String title) {
        if (!TextUtils.isEmpty(title)) {
            mViewHolder.centerTv.setText(title);
        }
    }

    public void setRightTitle(String text) {
        if (!TextUtils.isEmpty(text)) {
            mViewHolder.rightTv.setText(text);
        }
    }

    public void setLeftTitle(String text) {
        mViewHolder.leftTv.setText(text);
    }
    public void setRightIcon(int sourceID) {
        mViewHolder.rightIv.setImageResource(sourceID);
    }
    public void setAppTitleTextSize(float size) {
        mViewHolder.centerTv.setTextSize(size);
    }
    /**
     * 设置中间标题颜色
     *
     * @param color
     */
    public void setAppTitleTextColor(int color) {
        mViewHolder.centerTv.setTextColor(getResources().getColor(color));
    }

    public void setLeftIcon(int sourceID) {
        mViewHolder.leftIv.setImageResource(sourceID);
    }

    public void setAppBackgroundColor(int color) {
        viewAppTitle.setBackgroundColor(getResources().getColor(color));
    }

    public void setRightTvTextColor(int color) {
        mViewHolder.rightTv.setTextColor(getResources().getColor(color));
    }

    public void setAppBackgroundDrawable(int drawable) {
        viewAppTitle.setBackgroundColor(drawable);
    }
    public void setAppBackgroundResources(int resources) {
        viewAppTitle.setBackgroundResource(resources);
    }
    public void setOnLeftButtonClickListener(OnLeftButtonClickListener listen) {
        mLeftButtonClickListener = listen;
    }

    public void setOnRightButtonClickListener(OnRightButtonClickListener listen) {
        mRightButtonClickListener = listen;
    }

    public interface OnLeftButtonClickListener {
        void onLeftButtonClick(View v);
    }

    public interface OnRightButtonClickListener {
        void OnRightButtonClick(View v);
    }
}

